package com.web.image.download;

import java.awt.Image;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;

public class ConnectImage {

	private String protocol;
	private String host;
	private int port;
	private String file;
	private String path;
	private String filename;
	
	public ConnectImage(String protocol, String host, int port, String file, String path, String filename) {
		super();
		this.protocol = protocol;
		this.host = host;
		this.port = port;
		this.file = file;
		this.path = path;
		this.filename = filename;
	}
	
	public String getProtocol() {
		return protocol;
	}
	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}
	public String getHost() {
		return host;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
	public String getFile() {
		return file;
	}
	public void setFile(String file) {
		this.file = file;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getFilename() {
		return filename;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}

	public void test(){
		
		try {
			URL server = new URL(protocol, host, 80, file);
			
			HttpURLConnection connection = (HttpURLConnection)server.openConnection();
			InputStream is = null;
			OutputStream os = null;
			 
			connection.setRequestMethod("GET");
			connection.connect();
			is = connection.getInputStream();
			os = new FileOutputStream(path + "" + filename);

			byte[] buffer = new byte[1024];
			int byteReaded = is.read(buffer);
			while(byteReaded != -1)
			{
				os.write(buffer,0,byteReaded);
				byteReaded = is.read(buffer);
			}
			
			os.close();
		} 
		catch (ProtocolException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {	
			e.printStackTrace();
		}
		finally {
			System.out.println("Image Down End");
		}
	
	}
	
}
